package com.knowledge.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.knowledge.dao.entity.ArticleTag;
import com.knowledge.dao.entity.Tag;
import com.knowledge.dao.mapper.ArticleTagMapper;
import com.knowledge.dao.mapper.TagMapper;
import com.knowledge.service.TagService;
import com.knowledge.vo.Result;
import com.knowledge.vo.TagVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author zzz
 * @create 2022-02-10 17:01
 */
@SuppressWarnings("all")
@Service
public class TagServiceImpl implements TagService {
    @Autowired
    private TagMapper tagMapper;
    @Autowired
    private ArticleTagMapper articleTagMapper;

    @Override
    public List<TagVo> findTagsByArticleId(Long articleId) {
        //mybatis-plus 提供的baseMapper无法进行多表查询 需要自己写
        List<Tag> tags = tagMapper.findTagsByArticleId(articleId);
        return  copyList(tags);
    }

    @Override
    public Result findsHots(int limit) {
        /**
         * 1.标签所拥有的文章数量最多 为最热标签
         * 2.查询 根据tag_id 分组计数 从大到校 排列取值 limit个
         */
        List<Long> tagIds = tagMapper.findHotsTagIds(limit);
        if(CollectionUtils.isEmpty(tagIds)){
            return Result.success(Collections.emptyList());
        }
        //需求的是tagId 和  tagName Tag对象
        List<Tag> tagList = tagMapper.findTagsByTagsIds(tagIds);
        return Result.success(tagList);

    }

    @Override
    public Result findAll() {
        LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(Tag::getId,Tag::getTagName);
        List<Tag> tagList = tagMapper.selectList(queryWrapper);
        return Result.success(copyList(tagList));
    }

    @Override
    public int save(ArticleTag articleTag) {
        return articleTagMapper.insert(articleTag);
    }

    @Override
    public Result findAllDetail() {
        List<Tag> tagList = tagMapper.selectList(new LambdaQueryWrapper<>());
        return Result.success(copyList(tagList));
    }

    @Override
    public Result findTagDetailById(Long id) {
        Tag tag = tagMapper.selectById(id);
        TagVo tagVo = copy(tag);
        return Result.success(tagVo);
    }

    private List<TagVo> copyList(List<Tag> tags) {
        List<TagVo> tagVoList = new ArrayList<>();
        for (Tag tag : tags) {
            tagVoList.add(copy(tag));
        }
        return tagVoList;
    }

    private TagVo copy(Tag tag){
        TagVo tagVo = new TagVo();
        BeanUtils.copyProperties(tag,tagVo);
        tagVo.setId(String.valueOf(tag.getId()));
        return tagVo;
    }

    @Override
    public Tag getOrSaveTag(String tagName) {

        Tag tag = findTagByTagName(tagName);
        if (tag == null) {
            tag = new Tag();
            tag.setTagName(tagName);
            tag.setAvatar("");
            tagMapper.insert(tag);
        }
        return tag;
    }

    private Tag findTagByTagName(String tagName) {

        if (StrUtil.isBlank(tagName)) {
            return null;
        }
        LambdaQueryWrapper<Tag> query = Wrappers.<Tag>lambdaQuery().eq(Tag::getTagName, tagName);

        return tagMapper.selectOne(query);
    }
}
